package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;

public abstract class Overlay extends View implements Drawable {

	private ArrayList<GameButton> buttons;
	private boolean visible;
	private Dimension size;
	
	public Overlay( Dimension size ) {
		this.size = size;
		this.visible = true;
		this.buttons = new ArrayList<GameButton>();
	}
	
	public final void draw( Graphics g ) {
		if(visible) {
			drawOverlay( g );
			drawButtons( g );
		}//if it is not visible, do not draw
	}
	
	protected abstract void drawOverlay( Graphics g );
	
	public Dimension getOuterDimension() {
		return size;
	}
	
	public void setOuterDimension(Dimension dimension) {
		this.size = dimension;//change the stat
		this.onResize();//then tell child to update as needed 
	}
	
	protected abstract void onResize();
	
	public void makeVisisble() {
		this.visible = true;//and now 
		addGameButtons();
	}
	
	public void makeInvisible() {
		this.visible = false;
		removeGameButtons();
	}
	
	protected void addButton( GameButton button ) {
		this.buttons.add(button);
		GameButtonRegistry.getInstance().addGameButton( button );
	}
	
	protected void removeButton( GameButton button ) {
		this.buttons.remove( button );
		GameButtonRegistry.getInstance().removeGameButton( button );
	}
	
	protected void removeAllButtons() {
		removeGameButtons();
		this.buttons.clear();
	}
	
	private void addGameButtons() {
		for( GameButton b : buttons ) {
			GameButtonRegistry.getInstance().addGameButton(b);
		}
	}
	
	private void drawButtons( Graphics g ) {
		for( GameButton b : this.buttons ) {
			b.draw( g );
		}
	}
	
	private void removeGameButtons() {
		for( GameButton b : buttons ) {
			GameButtonRegistry.getInstance().removeGameButton(b);
		}
	}
}
